package com.bwie.goods.service.impl;

import com.bwie.goods.util.MD5Util;
import com.bwie.miaosha.common.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.miaosha.common.utils.Query;

import com.bwie.goods.dao.UserDao;
import com.bwie.goods.entity.UserEntity;
import com.bwie.goods.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserEntity login(UserEntity user) {
        UserEntity login = baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("nickname",user.getNickname()));
        if (login!=null) {
            String password = MD5Util.md5(user.getPassword());
            if (login.getPassword().equals(password)) {
                return login;
            }
        }
        return null;
    }

    @Override
    public void register(UserEntity user) {
        //加盐
        String password = MD5Util.md5(user.getPassword());
        //烟值
        user.setSalt("1a2b3c");
        //赋盐值
        user.setPassword(password);
        Date register_date = new Date();
        user.setRegisterData(register_date);
        user.setLoginCount(0);
        baseMapper.insert(user);
    }

}